﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Telerik.Windows.Controls;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCM.UI.ChildWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;

namespace Baosight.ZNH.ZMCM.UI
{
    /// <summary>  
    /// Title:ZMCME0  出库管理
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 余虎
    /// Version: 1.0
    /// History:
    ///       2011-7-5  余虎   [创建]
    ///<summary>  
    public partial class ZMCME0 : EFViewCmes
    { 
        private string fdNoteCode;
        private EIBlock empBlock;
        public ZMCME0()
        {
            InitializeComponent();
        }
        public override void Initialize(EIInfo initData)
        {
            initData.AddBlock((new ExportMaterialBlock()).GetExportMaterialBlock());
            fdNoteCode = initData.Get("fdNoteCode").ToString();
            empBlock = initData.GetBlock("cmes.cm.emp");
            base.Initialize(initData);
           
        }

        /// <summary>
        /// 主表数据改变，从表改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grid_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            if (null != this.grid.SelectedItem)
            {
                EIRow _row = (this.grid.SelectedItem as UIBaseEntity).GetEIRow();

                Dictionary<string, string> args = new Dictionary<string, string>();
                args.Add("fdExportmaterialId", _row["fdKeyId"].ToString());
                this.container.LoadView<EFCustomViewContainerPanel>("ZMCME1", args);
            }
            else
            {
                this.container.Content = null;
            }
        }
        #region 出库/冲红
        /// <summary>
        /// 出库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "出库", "insert", null, sender, e);
        }
        /// <summary>
        /// 冲红
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnBluntRedButtonClick(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;
            int  count = this.grid.GetCheckedEIRows().Count;
            EIInfo eiInfo = new EIInfo();
            if(count <1)
            {
                base.OpenEFWindowAlert("请选择一条记录",true);
                return;
            }
            else if (count != 1)
            {
                base.OpenEFWindowAlert("一次只能选择一条记录", true);
                return;
            }
            else
            {
                EIRow row = this.grid.GetCheckedEIRows()[0];
                if (row["fdExportType"].ToString().Equals("红冲"))
                {
                    base.OpenEFWindowAlert("选中的单据为红冲单据，不能进行红冲操作", true);
                    return;
                }
                DialogParameters param = this.ModelDialogParameters("Confirm", "确定要进行红冲出库操作吗？");
                param.Closed = RedRushedButtenClick;
                EFWindow.Confirm(param);
                
            }
        }
        /// <summary>
        /// 红冲
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RedRushedButtenClick(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == null || !e.DialogResult.Value)
            {
                this.TempButtonOpen(true);//取消后，按钮生效
                return;
            }
            if (e.DialogResult.Value)
            {
                base.Submit(OperationMode.Insert, "ZMCME0", "insert", DataRange.CheckedItems, DoCommitCompleted, null);
            }
        }
        /// <summary>
        /// 执行红冲后刷新页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void DoCommitCompleted(object sender, DoWorkCompletedEventArgs e)
        {

            if (e.EIInfo.Status == -1)
            {
                String msg = "插入失败！";
                if (e.EIInfo.GetMsg() != null || e.EIInfo.GetMsg().Equals(""))
                {
                    msg = e.EIInfo.Msg;
                }
                base.OpenEFWindowAlert(msg, true);
                base.TempButtonOpen(true);
            }
            else
            {
                this.grid.Refresh();
                base.TempButtonOpen(true);
            }
        }

        #endregion
        #region 公共方法
        /// <summary>
        /// 
        /// </summary>
        /// <param name="_eIInfo"></param>
        /// <param name="_title"></param>
        /// <param name="_dialogType"></param>
        /// <param name="_args"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            ExportMaterialBlock block = new ExportMaterialBlock();
            OutBoundWindow outBoundWindow = new OutBoundWindow(_dialogType);
            outBoundWindow.ViewDataChanged += new EventHandler<ViewDataChangedEventArgs>(outBoundWindow_ViewDataChanged);//添加监听
            outBoundWindow.EIDataContext = new Platform.Core.UI.EIDataContext();
            _eIInfo.AddBlock(block.GetBusinessType());//业务类型下拉框
            _eIInfo.AddBlock(block.GetExportMaterialBlock1());//出库类型下拉框
            _eIInfo.AddBlock(block.GetMaterialType());//物资类型下拉框
            
            _eIInfo.AddBlock(empBlock);//出库人
            outBoundWindow.fdNoteCode.Value = fdNoteCode;
            outBoundWindow.fdDepotName.Value = this.EIDataContext.GetAttribute("fdDepotName");
            outBoundWindow.fdDepotCode.Value = this.EIDataContext.GetAttribute("fdDepotCode");

            if ("insert".Equals(_dialogType))
            {
                (outBoundWindow.fdEditor.Content as CmesCustomView).Value = this.EIDataContext.GetAttribute("userId");
                (outBoundWindow.fdEditor.Content as CmesCustomView).Display = this.EIDataContext.GetAttribute("userName");
            }

            outBoundWindow.EIDataContext.Update(_eIInfo);

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小 
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = outBoundWindow;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(OperationWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OperationWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.grid.Refresh();
            }
            //按钮生效
            base.TempButtonOpen(true);
        }

        /// <summary>
        /// 给弹出窗口页面添加一个监听，为了获取弹出窗口中值改变后的新值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void outBoundWindow_ViewDataChanged(object sender, ViewDataChangedEventArgs e)
        {
            fdNoteCode = e.NewData.ToString();
        }
        #endregion
    }
}
